package com.freewater.springboot.demo.ioc.mutilconstructorautowired;

import lombok.Getter;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Getter
@ToString
public class BeanC {

    private BeanA beanA;
    private BeanB beanB;

    // required属性必须为false，因为spring要求一个类只能一个构造器使用required为true的@Autowired
    @Autowired(required = false)
    public BeanC(BeanA beanA) {
        this.beanA = beanA;
    }

    @Autowired(required = false)
    public BeanC(BeanB beanB) {
        this.beanB = beanB;
    }

    /**
     * spring会挑选依赖多的构造器
     */
    @Autowired(required = false)
    public BeanC(BeanA beanA, BeanB beanB) {
        this.beanA = beanA;
        this.beanB = beanB;
    }
}
